package renguanyu.app.codebuilder.view;

import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import renguanyu.app.codebuilder.util.CliBoardUtil;
import renguanyu.app.codebuilder.util.DaoFactory;

public class SaveTemplateBodyView extends AnchorPane {

    ScrollPane scrollPane = new ScrollPane();
    public VBox rowList = new VBox();
//    TemplateDao templateDao = new TemplateDao();
//    TemplateDataDao templateDataDao = new TemplateDataDao();
    ObservableMap<String, String> template;
    ObservableList<ObservableMap<String, String>> templateData;
    //页面模式，
    Boolean isView;


    public SaveTemplateBodyView(ObservableMap<String, String> template, ObservableList<ObservableMap<String, String>> templateData) {
        this.isView = false;
        this.template = template;
        this.templateData = templateData;
        initUI();
        initEvent();
    }
    public SaveTemplateBodyView(ObservableMap<String, String> template, ObservableList<ObservableMap<String, String>> templateData,boolean isView) {
        this.isView = isView;
        this.template = template;
        this.templateData = templateData;
        initUI();
        initEvent();
    }

    private void initUI() {

//        scrollPane.setFitToHeight(true);
        scrollPane.setFitToWidth(true);
        AnchorPane.setLeftAnchor(scrollPane,0.0);
        AnchorPane.setTopAnchor(scrollPane,0.0);
        AnchorPane.setRightAnchor(scrollPane,0.0);
        AnchorPane.setBottomAnchor(scrollPane,0.0);

        rowList.setSpacing(10.0);
        rowList.setPadding(new Insets(10.0));
        AnchorPane.setLeftAnchor(rowList,0.0);
        AnchorPane.setRightAnchor(rowList,0.0);

        if(template !=null){
            if(templateData!=null){
                for(int i=0;i<templateData.size();i++){
                    ObservableMap<String, String> templateDataItem = templateData.get(i);
                    String tempDataId = templateDataItem.get("TEMP_DATA_ID");
                    String fileName = templateDataItem.get("F_NAME");
                    String fileData = templateDataItem.get("F_DATA");
                    AnchorPane row = createOneRow(tempDataId,fileName,fileData);
                    rowList.getChildren().addAll(row);
                }
            }


        }
        else{
            appendOneRow();
        }

        scrollPane.setContent(rowList);
        this.getChildren().addAll(scrollPane);
        this.setPrefHeight(500.0);
        this.setMinWidth(700.0);

    }

    public void appendOneRow() {
        AnchorPane row = createOneRow(null, null,null);
        rowList.getChildren().add(row);
    }

    private AnchorPane createOneRow(String tempDataId, String key, String value) {

        AnchorPane row = new AnchorPane();

        TextField fileNameTextField = new TextField();
        if(key!=null){
            fileNameTextField.setText(key);
        }
        fileNameTextField.getStyleClass().add("fileNameTextFieldCss");
        fileNameTextField.setPromptText("文件名，例如：Action.java");
        AnchorPane.setLeftAnchor(fileNameTextField,10.0);
        AnchorPane.setTopAnchor(fileNameTextField,10.0);

        TextArea fileDataTextArea = new TextArea();
        if(value!=null){
            fileDataTextArea.setText(value);
        }
        fileDataTextArea.getStyleClass().add("fileDataTextAreaCss");
        fileDataTextArea.setPromptText("文件模板数据，例如：Action.java里面的代码和标记");
        fileDataTextArea.setPrefHeight(180.0);
        fileDataTextArea.setPrefWidth(447.0);
        AnchorPane.setLeftAnchor(fileDataTextArea,180.0);
        AnchorPane.setTopAnchor(fileDataTextArea,10.0);
        AnchorPane.setRightAnchor(fileDataTextArea,60.0);
        AnchorPane.setBottomAnchor(fileDataTextArea,10.0);

        if(isView){
            Button copyButton = new Button("复制");
            copyButton.setOnAction(event -> {
                copyRow(event);
            });
            AnchorPane.setTopAnchor(copyButton,10.0);
            AnchorPane.setRightAnchor(copyButton,10.0);
            row.getChildren().addAll(fileNameTextField,fileDataTextArea,copyButton);
        }
        else{
            Button removeButton = new Button("移除");
            if(template!=null){
                removeButton.setId(tempDataId);
            }
            removeButton.getStyleClass().add("tempDataIdTextAreaCss");
            removeButton.setOnAction(event -> {
                removeRow(event);
            });
            AnchorPane.setTopAnchor(removeButton,10.0);
            AnchorPane.setRightAnchor(removeButton,10.0);
            row.getChildren().addAll(fileNameTextField,fileDataTextArea,removeButton);
        }
        return row;
    }

    private void copyRow(ActionEvent event) {
        Button source = (Button) event.getSource();
        AnchorPane row = (AnchorPane) source.getParent();
        TextArea fileDataTextArea = (TextArea) row.getChildren().get(1);
//        System.out.println(fileDataTextArea.getText());
        CliBoardUtil.copyString(fileDataTextArea.getText());
    }

    private void removeRow(ActionEvent event) {
        Button source = (Button) event.getSource();
        String templateDataId = source.getId();
        if(templateDataId!=null){
            boolean flag = DaoFactory.getTemplateDataDao().delete(templateDataId);
        }
        rowList.getChildren().remove(source.getParent());
    }

    private void initEvent() {
    }



}
